﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.18
 * 描 述：打印设置
 */
var companyId = request('companyId');
var ztId = request("ztId");
var acceptClick;
var keyValue = '';
var bootstrap = function ($, learun) {
    "use strict";
    var selectedRow = config.getParent(learun).selectedRow;
    var page = {
        init: function () {
            page.bind();
            page.initData();
        },
        bind: function () {
            // 帐套选择
            $('#ztId').lrselect({
                type: 'default',
                //placeholder: '请选择帐套',
                // 展开最大高度
                maxHeight: 300,
                allowSearch: true
            });
            $('#ztId').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'gzzts/dropdownlist?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {companyId: companyId},
                select: function (ztItem) {
                    if (ztItem != null && !selectedRow) {
                        page.inittree();
                    }
                }
            });

            $('#reloadTree').on('click', function (e) {
                page.inittree();
            });


            // 全选
            $('#select_all').on('click', function (e) {
                $('.lr-tree-node-cb').trigger('click');
            });

            // 全不选
            $('#select_none').on('click', function (e) {
                $('#itemTree').lrtreeSet('allNoCheck');
            });
        },
        initData: function () {
            if (!!selectedRow) {
                keyValue = selectedRow.id;
                $('#companyId').val(selectedRow.companyId);
                $("#name").val(selectedRow.name);
                $("#ztId").lrselectSet(selectedRow.ztId);
                page.inittree(selectedRow.ztId);

                if(!!selectedRow.columns) {
                    setTimeout(function () {
                        var cols = selectedRow.columns.split(",");
                        for (var i = 0; i < cols.length; i++) {
                            var $div = $('.lr-tree-root').find('div[title=' + cols[i] + ']');
                            $div.find('.lr-tree-node-cb').trigger('click');
                        }
                    }, 200);
                }

            } else {
                $('#companyId').val(companyId);
            }
        },
        inittree: function (ztId) {
            if (!ztId) {
                ztId = $('#ztId').lrselectGet();
            }
            if (companyId != "" && ztId != "") {
                $("#itemTree").removeAttr("lr-tree");
                $("#itemTree").removeAttr("lr-scroll-wrap");
                $("#itemTree").empty();
                $('#itemTree').lrtree({
                    url: config.base_server() + 'gzitems/dropdownlist2?companyId=' + companyId + '&ztId=' + ztId + '&access_token=' + config.getToken().access_token,
                    param: {}
                });
            }
        },
    };
    // 保存数据
    acceptClick = function (callBack) {
        if (!$('#form').lrValidform()) {
            return false;
        }
        var postData = $('#form').lrGetFormData(keyValue);
        postData.companyId = companyId;
        postData.ztId = $('#ztId').lrselectGet();
        postData.name = $('#name').val();
        postData.columns = $("#itemTree").lrtreeSet('getCheckNodeIds').toString();

        $.lrSaveForm(config.base_server() + 'gzitemprints/saveOrUpdate?access_token=' + config.getToken().access_token + '&keyValue=' + keyValue, JSON.stringify(postData), function (res) {
            // 保存成功后才回调
            if (!!callBack) {
                callBack();
            }
        });
    };
    page.init();
}